package com.lx.zy.project.system.service.impl;

import com.lx.zy.common.constant.UserConstants;
import com.lx.zy.common.exception.CustomException;
import com.lx.zy.common.utils.StringUtils;
import com.lx.zy.framework.aspectj.lang.annotation.DataScope;
import com.lx.zy.framework.web.domain.TreeSelect;
import com.lx.zy.project.system.domain.SysDept;
import com.lx.zy.project.system.mapper.SysDeptMapper;
import com.lx.zy.project.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.tree.TreePath;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: zy-vue
 * @description:
 * @author: Lx
 * @create: 2019-11-04 01:59
 **/
@Service
public class SysDeptServiceImpl implements ISysDeptService
{

    @Autowired
    private SysDeptMapper deptMapper;

    /**
     * 查询部门管理数据
     *
     * @param dept 部门信息
     * @return 部门信息集合
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<SysDept> selectDeptList(SysDept dept)
    {
        return deptMapper.selectDeptList(dept);
    }

    /**
     * 构建前端所需要树结构
     *
     * @param depts 部门列表
     * @return 树结构列表
     */
    @Override
    public List<SysDept> buildDeptTree(List<SysDept> depts)
    {
        List<SysDept> returnList = new ArrayList<>();
        for (Iterator<SysDept> iterator = depts.iterator(); iterator.hasNext(); )
        {
            SysDept t = iterator.next();
            //根据传入的某个父节点Id,遍历该父节点的所有子节点
            if (t.getParentId() == 0)
            {
                recursionFn(depts, t);
                returnList.add(t);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = depts;
        }
        return returnList;
    }

    /**
     * 构建前端所需要下拉树结构
     *
     * @param depts 部门列表
     * @return 下拉树结构列表
     */
    @Override
    public List<TreeSelect> buildDeptTreeSelect(List<SysDept> depts)
    {
        List<SysDept> deptTrees = buildDeptTree(depts);
        return deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 校验部门名称是否唯一
     *
     * @param dept 部门信息
     * @return 结果
     */
    @Override
    public String checkDeptNameUnique(SysDept dept)
    {
        Long deptId = StringUtils.isNull(dept.getDeptId()) ? -1L : dept.getDeptId();
        SysDept deptInfo = deptMapper.checkDeptNameUnique(dept.getDeptName(), dept.getParentId());
        if (StringUtils.isNotNull(deptInfo) && deptInfo.getParentId().longValue() != deptId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 新增保存部门信息
     *
     * @param dept 部门信息
     * @return 结果
     */
    @Override
    public int insertDept(SysDept dept)
    {
        //父类部门信息
        SysDept parentDeptInfo = deptMapper.selectDeptById(dept.getParentId());
        if (!UserConstants.DEPT_NORMAL.equals(parentDeptInfo.getStatus()))
        {
            throw new CustomException("部门停用，不允许新增");
        }
        dept.setAncestors(parentDeptInfo.getAncestors() + "," + parentDeptInfo.getParentId());
        return deptMapper.insertDept(dept);
    }

    private void recursionFn(List<SysDept> list, SysDept t)
    {
        List<SysDept> childList = getChildList(list, t);
        t.setChildren(childList);
        for (SysDept tChild : childList)
        {
            if (hashChild(list, tChild))
            {
                Iterator<SysDept> iterator = childList.iterator();
                while (iterator.hasNext())
                {
                    SysDept n = iterator.next();
                    recursionFn(list, n);
                }
            }
        }
    }

    /**
     * 得到子节点列表
     *
     * @param list
     * @param t
     * @return
     */
    private List<SysDept> getChildList(List<SysDept> list, SysDept t)
    {
        List<SysDept> tlist = new ArrayList<>();
        Iterator<SysDept> iterator = list.iterator();
        while (iterator.hasNext())
        {
            SysDept dept = iterator.next();
            if (dept.getParentId().longValue() == t.getDeptId().longValue())
            {
                tlist.add(dept);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     *
     * @param list
     * @param t
     * @return
     */
    private boolean hashChild(List<SysDept> list, SysDept t)
    {
        return getChildList(list, t).size() > 0 ? true : false;
    }
}
